class ScheduledTasksController < ApplicationController
  
  load_and_authorize_resource

  before_filter :load_data

  def load_data
    @tasks = Task.all(:order => :name)
    @frequencies = [Frequency::ASAP, Frequency::HALF_HOURLY, Frequency::HOURLY, Frequency::FOUR_TIMES_A_DAY, Frequency::TWICE_A_DAY, Frequency::DAILY,  Frequency::WEEKLY, Frequency::FORTNIGHTLY,  Frequency::MONTHLY]
  end

  # GET /scheduled_tasks
  # GET /scheduled_tasks.xml
  def index
    save_redirect_back request.request_uri

    @scheduled_tasks = search(params[:search_name], params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @scheduled_tasks }
    end
  end

  # GET /scheduled_tasks/1
  # GET /scheduled_tasks/1.xml
  def show
    @scheduled_task = ScheduledTask.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @scheduled_task }
    end
  end

  # GET /scheduled_tasks/new
  # GET /scheduled_tasks/new.xml
  def new
    @scheduled_task = ScheduledTask.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @scheduled_task }
    end
  end

  # GET /scheduled_tasks/1/edit
  def edit
    @scheduled_task = ScheduledTask.find(params[:id])
  end

  # POST /scheduled_tasks
  # POST /scheduled_tasks.xml
  def create
    @scheduled_task = ScheduledTask.new(params[:scheduled_task])

    respond_to do |format|
      if @scheduled_task.save
        flash[:notice] = t('msg_record_successfully_created')
        format.html { redirect_to(@scheduled_task) }
        format.xml  { render :xml => @scheduled_task, :status => :created, :location => @scheduled_task }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @scheduled_task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /scheduled_tasks/1
  # PUT /scheduled_tasks/1.xml
  def update
    @scheduled_task = ScheduledTask.find(params[:id])

    respond_to do |format|
      if @scheduled_task.update_attributes(params[:scheduled_task])
        flash[:notice] =  t('msg_record_successfully_updated')
        format.html { redirect_to(@scheduled_task) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @scheduled_task.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /scheduled_tasks/1
  # DELETE /scheduled_tasks/1.xml
  def destroy
    @scheduled_task = ScheduledTask.find(params[:id])
    @scheduled_task.destroy

    respond_to do |format|
      format.html { redirect_to(scheduled_tasks_url) }
      format.xml  { head :ok }
    end
  end

  def redirect_back
      redirect_back_or_default params[:url]
  end

  #Pongo aca este metodo porque no quiero tocar el modelo que pertenece al plugin
  def search(search_task_name, page)
    items_per_page = count_record_per_page :scheduled_tasks
    order = "tasks.name"

    conditions_clauses = []
    conditions_options = []

    if !search_task_name.nil? && search_task_name.size != 0
      conditions_clauses << "upper(tasks.name) like upper(?)"
      conditions_options << "%#{search_task_name}%"
    end
    conditions = [conditions_clauses.join(" AND "), conditions_options].flatten

    scheduled_tasks = ScheduledTask.paginate :per_page => items_per_page,:page => page, :include => :task, :conditions => conditions, :order => order
    return scheduled_tasks
  end

end
